const jwt = require("jsonwebtoken");
const { promisify } = require("util");
const { uuid } = require("../config/config.default");
const tojwt = promisify(jwt.sign);
const verfiy = promisify(jwt.verify);

module.exports.verifyToken = function (require = true) {
  return async (req, res, next) => {
    const token = req.header.authorization;
    token = token ? token.split("Bearer ")[1] : null;
    if (token) {
      try {
        let userInfo = await verfiy(token, uuid);
        req.user = userInfo;
        next();
      } catch (error) {
        res.status("402").json(error);
      }
    } else if (require) {
      res.status("402").json(error);
    } else {
      next();
    }
  };
};

module.exports.createToken = async (userInfo) => {
  const token = await tojwt({ userInfo }, uuid, {
    expiresIn: 60 * 60 * 24,
  });
  return token
};


var token = jwt.sign({foo:'hello'},'555')
console.log(token);